﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonTools;
using CommonTools.Controls;
using Gtong.BLL.BasicInfo;
using Gtong.MODEL;
using Gtong.Properties;
using shop;
using WeifenLuo.WinFormsUI.Docking;

namespace Gtong.BasicInfo
{
    public partial class SystemProfileForm : DockContent
    {
        BLL.BasicInfo.JSystemProfileBll _bll  = new JSystemProfileBll();

        public SystemProfileForm()
        {
            InitializeComponent();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            JSystemProfile profile = new JSystemProfile()
            {
                Company = this.box_CompanyName.Text,
                Address = this.box_Address.Text,
                OpeningBank = this.box_OpeningBank.Text,
                BankAC = this.box_BankAC.Text,
                TaxRegistrationNo = this.box_TaxRegistrationNo.Text,
                Fax = box_FaxNumber.Text,
                Tel = box_PhoneNumber.Text,
                StartUsingYear = (int) box_StartUsingYear.Value,
                StartUsingPeriod = (int) box_StartUsingPeriod.Value,
                CurrentPeriod= (int)box_VoucherCurrentPeriod.Value,
                CurrentYear= (int)box_VoucherCurrentYear.Value,
                QtyAccuracy = (int) box_QtyAccuracy.Value,
                UPAccuracy = (int) box_UPAccuracy.Value,
                DocListMaxRow = (int) box_DocListMaxrownumber.Value,
                OnlyOwnDocEditable = box_OnlyOwnDocEditable.Checked,
                LocalCurrencyCode = box_LocalCurrencyCode.Text,
                LocalCurrencyName = box_LocalCurrencyName.Text,
                LocalCurrencyDecimals = (int)box_LocalCurrencyDecimals.Value,
                AcsetEnable = this.box_VoucherEnable.Checked,
                CurrentYearProfits = this.box_CurrentYearProfitAccount.Text,
                CurrentYearProfitsId = this._currentYearProfitAccountId,
                ProfitDistibution = this.box_ProfitAccount.Text,
                ProfitDistibutionId = this._profitAccountId,
                PriorYearIncomeAdjustment = this.box_PriorYearIncomeAdjustment.Text,
                PriorYearIncomeAdjustmentId = JSystemProfile.Profile.PriorYearIncomeAdjustmentId,
                
            };

            if (_bll.SaveProfile(profile, out var err))
            {
                MessageTip.ShowOk(this, CN.Msg_Save);
            }
            else
            {
                MessageBox.Show(err, Resources.systemInformation, MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SystemProfileForm_Load(object sender, EventArgs e)
        {
            //加载设置
            this.LoadProfile();
        }


        private void LoadProfile()
        {

            JSystemProfile.Profile = _bll.QueryProfile();

            this.box_CompanyName.Text = JSystemProfile.Profile.Company;
            this.box_Address.Text = JSystemProfile.Profile.Address;
            this.box_OpeningBank.Text = JSystemProfile.Profile.OpeningBank;
            this.box_BankAC.Text = JSystemProfile.Profile.BankAC;
            this.box_TaxRegistrationNo.Text = JSystemProfile.Profile.TaxRegistrationNo;
            this.box_FaxNumber.Text = JSystemProfile.Profile.Fax;
            this.box_PhoneNumber.Text = JSystemProfile.Profile.Tel;
            this.box_StartUsingYear.Value = JSystemProfile.Profile.StartUsingYear;
            this.box_StartUsingPeriod.Value = JSystemProfile.Profile.StartUsingPeriod;
            this.box_VoucherCurrentYear.Value = JSystemProfile.Profile.CurrentYear;
            this.box_VoucherCurrentPeriod.Value = JSystemProfile.Profile.CurrentPeriod;
            this.box_QtyAccuracy.Value = JSystemProfile.Profile.QtyAccuracy;
            this.box_UPAccuracy.Value = JSystemProfile.Profile.UPAccuracy;
            this.box_DocListMaxrownumber.Value = JSystemProfile.Profile.DocListMaxRow;
            this.box_OnlyOwnDocEditable.Checked = JSystemProfile.Profile.OnlyOwnDocEditable;
            this.box_LocalCurrencyCode.Text = JSystemProfile.Profile.LocalCurrencyCode;
            this.box_LocalCurrencyName.Text = JSystemProfile.Profile.LocalCurrencyName;
            this.box_LocalCurrencyDecimals.Value = JSystemProfile.Profile.LocalCurrencyDecimals;
            this.box_VoucherEnable.Checked = JSystemProfile.Profile.AcsetEnable;
            this.box_CurrentYearProfitAccount.Text = JSystemProfile.Profile.CurrentYearProfits;
            this._currentYearProfitAccountId = JSystemProfile.Profile.CurrentYearProfitsId;
            this.box_ProfitAccount.Text = JSystemProfile.Profile.ProfitDistibution;
            this._profitAccountId = JSystemProfile.Profile.ProfitDistibutionId;
            this.box_PriorYearIncomeAdjustment.Text = JSystemProfile.Profile.PriorYearIncomeAdjustment;

            //启用账套后 不能修改会计设置
            this.box_StartUsingYear.Enabled = this.box_StartUsingPeriod.Enabled = !JSystemProfile.Profile.AcsetEnable;
        }


        private string _profitAccountId = "";
        private string _currentYearProfitAccountId = "";

        /// <summary> 利润分配科目 </summary>
        private void box_ProfitAccount_RequestValidate(FastTextBox arg1, FastValueArgs arg2)
        {
            if (arg2.ChangeMode == ChangeModeEnum.Clear)
            {
                //将单据内容关联属性清空 
                this._profitAccountId = "";
                return;
            }

            Account model = FastBox.GetValue<JAccount, BLL.BasicInfo.JAccountBll>(arg1.Text, arg2.CallFilter);

            if (model == null)
            {
                arg2.Cancel = true;     //没有查询到结果, 还原输入内容 (这里不用修改关联内容了)
            }
            else
            {
                arg1.Text = model.Name ?? "";
                //将单据内容关联属性赋值 
                this._profitAccountId = model.Code;
            }
        }

        /// <summary> 本年利润科目 </summary>
        private void box_CurrentYearProfitAccount_RequestValidate(FastTextBox arg1, FastValueArgs arg2)
        {
            if (arg2.ChangeMode == ChangeModeEnum.Clear)
            {
                //将单据内容关联属性清空 
                this._currentYearProfitAccountId = "";
                return;
            }

            JAccount model = FastBox.GetValue<JAccount, BLL.BasicInfo.JAccountBll>(arg1.Text, arg2.CallFilter);

            if (model == null)
            {
                arg2.Cancel = true;     //没有查询到结果, 还原输入内容 (这里不用修改关联内容了)
            }
            else
            {
                arg1.Text = model.Name ?? "";
                //将单据内容关联属性赋值 
                this._currentYearProfitAccountId = model.Code;
            }
        }

        /// <summary> 以前年度损益调整 </summary>
        private void box_PriorYearIncomeAdjustment_RequestValidate(FastTextBox arg1, FastValueArgs arg2)
        {
            if (arg2.ChangeMode == ChangeModeEnum.Clear)
            {
                //将单据内容关联属性清空 
                JSystemProfile.Profile.PriorYearIncomeAdjustmentId = "";
                return;
            }

            JAccount model = FastBox.GetValue<JAccount, BLL.BasicInfo.JAccountBll>(arg1.Text, arg2.CallFilter);

            if (model == null)
            {
                arg2.Cancel = true;     //没有查询到结果, 还原输入内容 (这里不用修改关联内容了)
            }
            else
            {
                arg1.Text = model.Name ?? "";
                //将单据内容关联属性赋值 
                JSystemProfile.Profile.PriorYearIncomeAdjustmentId = model.Code;
            }
        }
    }
}
